<html>


<script>
// 但是用字符串不能保证常量是独特的，这样会引起一些问题：
// const COLOR_RED = "red";
// const COLOR_YELLOW = "yellow";
// const COLOR_BLUE = "blue";
// const MY_BLUE = "blue";

const COLOR_RED = Symbol("red");
const COLOR_YELLOW = Symbol("yellow");
const COLOR_BLUE = Symbol("blue");
 
function ColorException(message) {
   this.message = message;
   this.name = "ColorException";
}
function getConstantName(color) {
    switch (color) {
        case COLOR_RED :
            return "COLOR_RED";
        case COLOR_YELLOW :
            return "COLOR_YELLOW ";
        case COLOR_BLUE:
            return "COLOR_BLUE";
        default:
            throw new ColorException("Can't find this color");
    }
}
 
try {
//    let 
//    var color =  Symbol.keyFor("red") // green 引发异常
//    var colorName = getConstantName(color);
//    console.log(Symbol.keyFor(Symbol("red")))
let yellow1 = COLOR_YELLOW
// Symbol.keyFor(yellow1);    // "Yellow"
//    console.log(Symbol("red"))
console.log(COLOR_RED.description)
// console.log(COLOR_BLUE)
} catch (e) {
   var colorName = "unknown";
   console.log(e.message, e.name); // 传递异常对象到错误处理
}
</script>
</html>
